//
//  CountDownButton.m
//  TengYunWaystation
//
//  Created by Johnny on 15/5/7.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "CountDownButton.h"

@implementation CountDownButton

- (void)awakeFromNib {
    self.layer.cornerRadius = 15;
    self.clipsToBounds = YES;
}

- (void)startCountDown {
    [_timer invalidate];
    self.userInteractionEnabled = NO;
    self.enabled = NO;
    self.alpha = 0.8;
    _startDate = [NSDate date];
    _title = self.titleLabel.text;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownTime) userInfo:nil repeats:YES];
}

- (void)countDownTime {
    NSDate *currentDate = [NSDate date];
    NSInteger time = (NSInteger)[currentDate timeIntervalSinceDate:_startDate];
    if (time < 60) {
        NSString *title = [NSString stringWithFormat:@"%ld秒",(long)(60 - time)];
        [self setTitle:title forState:UIControlStateNormal];
    } else {
        self.alpha = 1;
        self.userInteractionEnabled = YES;
        self.enabled = YES;
        [self setTitle:_title forState:UIControlStateNormal];
        [_timer invalidate];
    }
}

- (void)dealloc {
    [_timer invalidate];
    _startDate = nil;
    _title = nil;
}

@end
